
import ImageAdItem from "./Items/ImageAdItem";
import { ui } from "../../../../ui/layaMaxUI";
import { BaseAdView } from "./BaseAdView";
import { AdConfigData } from "./ShiyouAdManager";


export default class CenterAdView extends ui.ShiyouAdView.CenterAdViewUI implements BaseAdView {
    private m_size = 50;


    OnPause() {
    }

    OnResume() {
    }

    constructor() {
        super();
        Laya.stage.addChild(this);
        this.m_AdList.on(Laya.Event.MOUSE_DOWN, this, () => { Laya.timer.clearAll(this); });
        this.m_AdList.on(Laya.Event.MOUSE_UP, this, this.OnAdListClick);
        this.m_AdList.on(Laya.Event.MOUSE_OUT, this, this.OnAdListClick);
        this.visible = false;
    }

    createChildren() {
        super.createChildren();
        this.m_AdList.renderHandler = new Laya.Handler(this, this.OnRenderHandler);
        this.m_AdList.elasticEnabled = true;
        this.m_AdList.selectEnable = true;
        this.m_AdList.repeatX = 5;
        this.m_AdList.repeatY = 2;
        this.m_AdList.vScrollBarSkin = "";

    }

    public OnRenderHandler(cell: ImageAdItem, index: number) {
        let skindata = cell.dataSource;
        cell.SetData(skindata,"Type5",skindata.Type5);
    }

    InitData(addata:  Array<AdConfigData>) {
        if (addata.length < 1) return;

        let data = [];
        for (let index = 0; index < addata.length; index++) {
            const element = addata[index];
            if (element.Type5 !=null)
                data.push(element);
        }

        if (data.length < 1) return;

        let banneraddata = [];
        if (data.length < this.m_size) {
            let index = 0;
            for (; index < data.length;) {
                const element = data[index];
                banneraddata.push(element);
                if (banneraddata.length >= this.m_size)
                    break;

                if (index >= data.length - 1)
                    index = 0;
                else index++;
            }
        }
        else {
            banneraddata = data;
        }
        this.m_AdList.array = banneraddata;
        this.OnAdListClick();

    }



    public Show(centerY?, top?, bottom?) {
        if (centerY != null) {
            this.centerY = centerY;
        }
        if (top != null) {
            this.top = top;
        }
        if (bottom != null) {
            this.bottom = bottom;
        }
        this.visible = true;
        this.ListTweenToEnd();
    }

    public Hide() {
        this.visible = false;
        Laya.Tween.clearAll(this.m_AdList);
        Laya.timer.clearAll(this);
    }


    public ListTweenToEnd() {
        Laya.timer.once(1000, this, () => { this.m_AdList.tweenTo(this.m_AdList.length - 5, this.m_AdList.length * 300, Laya.Handler.create(this, this.ListTweenToStart)); })
    }

    ListTweenToStart() {
        this.m_AdList.tweenTo(0, this.m_AdList.length * 300, Laya.Handler.create(this, this.ListTweenToEnd));
    }

    OnAdListClick() {
        this.ListTweenToEnd();
    }

}